/*
 * @(#)ConfigMouseEvent.java   09/12/29
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.editor.config;

import org.jgraph.JGraph;
import org.jgraph.graph.BasicMarqueeHandler;

import fr.ema.arch3d.jmodel.configuration.Configuration;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class ConfigMouseEvent extends BasicMarqueeHandler implements MouseListener {

    /** Field description */
    public DrawConfigImpl dc;

    /**
     * Constructs ...
     *
     *
     * @param dc
     */
    public ConfigMouseEvent(DrawConfigImpl dc) {
        this.dc = dc;
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseClicked(MouseEvent e) {
        if ((e.getClickCount() == 1) && (e.getButton() == MouseEvent.BUTTON1)) {
            JGraph                graph  = getGraphForEvent(e);
            int                   x      = e.getX(),
                                  y      = e.getY();
            Object                cell   = graph.getFirstCellForLocation(x, y);
            Configuration config = dc.config;

            if ((cell != null) && cell.getClass().getSimpleName().equals("SpecVertex")) {
                for (int i = 0; i < config.getCompositeComponentClass().size(); i++) {
                    if (config.getCompositeComponentClass().get(i).getName().equals(cell.toString())) {
                        ConfigADLText t = new ConfigADLText();

                        dc.cText.setDocument(t.createTextCompositeCmp(config.getCompositeComponentClass().get(i)));
                        dc.cText.updateUI();
                    }
                }
            }
            if ((cell != null) && cell.getClass().getSimpleName().equals("SpecVertex")) {
                for (int i = 0; i < config.getPrimitiveComponentClass().size(); i++) {
                    if (config.getPrimitiveComponentClass().get(i).getName().equals(cell.toString())) {
                        ConfigADLText t = new ConfigADLText();

                        dc.cText.setDocument(t.createTextPrimitiveCmp(config.getPrimitiveComponentClass().get(i)));
                        dc.cText.updateUI();
                    }
                }
            }

            if ((cell != null) && cell.getClass().getSimpleName().equals("SpecEdge")) {
                for (int i = 0; i < config.getConnectorClass().size(); i++) {
                    if ((config.getConnectorClass().get(i)).getName().equals(
                            cell.toString())) {
                        ConfigADLText t = new ConfigADLText();

                        dc.cText.setDocument(
                            t.createTextCnt(config.getConnectorClass().get(i)));
                        dc.cText.updateUI();
                    }
                }
            } else {

                // System.out.println("here");
                ConfigADLText t = new ConfigADLText();

                dc.cText.setDocument(t.createTextConfig(config));
                dc.cText.updateUI();
            }
        }
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseEntered(MouseEvent e) {
        if ((e.getClickCount() == 1) && (e.getButton() == MouseEvent.BUTTON1)) {}
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseExited(MouseEvent e) {

        // TODO Auto-generated method stub
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mousePressed(MouseEvent e) {
        if ((e.getClickCount() == 1) && (e.getButton() == MouseEvent.BUTTON3)) {

            // startPoint = e.getPoint();
            JGraph graph = getGraphForEvent(e);

            // CellView[] views = graph.getGraphLayoutCache().getCellViews();
            // Get Cell under Mousepointer
            int    x    = e.getX(),
                   y    = e.getY();
            Object cell = graph.getFirstCellForLocation(x, y);

            // Print Cell Label
            if ((cell != null) && cell.getClass().getSimpleName().equals("SpecVertex")) {
                JPopupMenu menu1 = new JPopupMenu();

                // Display PopupMenu
                JMenuItem item1;

                menu1.add(item1 = new JMenuItem("Replace component"));
                menu1.add(item1 = new JMenuItem("Delete component"));
                item1.setHorizontalTextPosition(JMenuItem.RIGHT);

                // item.addActionListener(menuListener);
                menu1.show(graph, e.getX(), e.getY());
            } else if ((cell != null) && cell.getClass().getSimpleName().equals("SpecEdge")) {
                JPopupMenu menu1 = new JPopupMenu();

                // Display PopupMenu
                JMenuItem item1;

                menu1.add(item1 = new JMenuItem("Delete connector"));
                item1.setHorizontalTextPosition(JMenuItem.RIGHT);

                // item.addActionListener(menuListener);
                menu1.show(graph, e.getX(), e.getY());
            } else {
                JPopupMenu menu = new JPopupMenu();

                // Display PopupMenu
                JMenuItem item;

                menu.add(item = new JMenuItem("Insert component"));
                menu.add(item = new JMenuItem("Insert connector"));
                item.setHorizontalTextPosition(JMenuItem.RIGHT);

                // item.addActionListener(menuListener);
                menu.show(graph, e.getX(), e.getY());
            }
        }
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseReleased(MouseEvent e) {

        // TODO Auto-generated method stub
    }
}
